<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item :label="$t('calling.cnName')" prop="cnName">
        <el-input
            v-model="queryParams.cnName"
            :placeholder="$t('calling.cnNameInput')"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('calling.cardNo')" prop="cardNo">
        <el-input
            v-model="queryParams.cardNo"
            :placeholder="$t('calling.cardNoInput')"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('calling.phone')" prop="telphone">
        <el-input
            v-model="queryParams.telphone"
            :placeholder="$t('calling.phoneInput')"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('program.status')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('equipment.statusSelect')" clearable size="small">
          <el-option
              v-for="dict in statusOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('elButton.establishTime')">
        <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            :start-placeholder="$t('elButton.startTime')"
            :end-placeholder="$t('elButton.endTime')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
            $t('elButton.search')
          }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('elButton.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['customer_add']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
        >{{ $t('elButton.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['customer_edit']"
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
        >{{ $t('elButton.change') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['customer_del']"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDel"
        >{{ $t('elButton.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['customer_export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
        >{{ $t('elButton.export') }}
        </el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" :content="$t('elButton.refresh')" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark"
                    :content="showSearch ? $t('elButton.hideSearch') : $t('elButton.showSearch')" placement="top"
        >
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('equipment.number')" width="55" align="center" prop="id"/>
      <el-table-column :label="$t('calling.cnName')" align="center" prop="cnName"/>
      <!--<el-table-column label="英文名称" align="center" prop="enName"/>-->
      <el-table-column :label="$t('calling.age')" align="center" prop="age"/>
      <el-table-column :label="$t('calling.sex')" align="center" prop="sex">
        <template slot-scope="scope">
          <!-- <el-tag v-if="scope.row.sex === '0'">{{ $t('calling.secret') }}</el-tag> -->
          <el-tag v-if="scope.row.sex === '0'" type="success">{{ $t('calling.boy') }}</el-tag>
          <el-tag v-if="scope.row.sex === '1'" type="danger">{{ $t('calling.girl') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('calling.nation')" align="center" prop="nation"/>
      <el-table-column :label="$t('calling.cardNo')" align="center" prop="cardNo"/>
      <el-table-column :label="$t('calling.phone')" align="center" prop="telphone"/>
      <el-table-column :label="$t('calling.address')" align="center" prop="address"/>
      <el-table-column :label="$t('calling.isAlwaysFirst')" align="center" prop="isPrior" :formatter="isPriorFormat"/>
      <el-table-column :label="$t('program.status')" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column :label="$t('elButton.establishTime')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.control')" align="center" class-name="small-padding fixed-width"
                       width="150" fixed="right"
      >
        <template slot-scope="scope">
          <el-button
              v-hasPerm="['customer_edit']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
          >{{ $t('elButton.change') }}
          </el-button>
          <el-button
              v-hasPerm="['customer_del']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDel(scope.row)"
          >{{ $t('elButton.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.current"
        :limit.sync="queryParams.size"
        @pagination="getList"
    />

    <!-- 添加或修改客户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" :modal-append-to-body="true" :append-to-body="true"
               :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('calling.cnName')" prop="cnName">
              <el-input v-model="form.cnName" :placeholder="$t('calling.cnNameInput')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.enName')" prop="enName">
              <el-input v-model="form.enName" :placeholder="$t('calling.enNameInput')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.age')" prop="age">
              <el-input-number v-model="form.age" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.sex')">
              <el-select v-model="form.sex" :placeholder="$t('calling.sexSelect')">
                <!-- <el-option key="0" :label="$t('calling.secret')" value="0" /> -->
                <el-option key="1" :label="$t('calling.boy')" value="0"/>
                <el-option key="2" :label="$t('calling.girl')" value="1"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.nation')" prop="nation">
              <el-input v-model="form.nation" :placeholder="$t('calling.nationInput')"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item :label="$t('calling.avatr')" prop="portrait">
              <el-input v-model="form.portrait" type="textarea" :placeholder="$t('journal.journalInfoInput')"/>
            </el-form-item>
          </el-col>-->
          <!--<el-col :span="12">
            <el-form-item label="证件类型">
              <el-select v-model="form.cardType" placeholder="请选择证件类型">
                <el-option label="请选择字典生成" value=""/>
              </el-select>
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item :label="$t('calling.cardNo')" prop="cardNo">
              <el-input v-model="form.cardNo" :placeholder="$t('calling.cardNoInput')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.address')" prop="address">
              <el-input v-model="form.address" :placeholder="$t('calling.addressInput')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.phone')" prop="telphone">
              <el-input v-model="form.telphone" :placeholder="$t('calling.phoneInput')"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('calling.synopsis')" prop="introduce">
              <el-input v-model="form.introduce" type="textarea" :placeholder="$t('calling.synopsisInput')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.isAlwaysFirst')">
              <el-radio-group v-model="form.isPrior">
                <el-radio
                    v-for="dict in isPriorOptions"
                    :key="dict.value"
                    :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!--<el-col :span="12">
            <el-form-item :label="$t('program.status')">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('submitText') }}</el-button>
        <el-button @click="cancel">{{ $t('cancelText') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomer, getCustomer, delCustomer, addCustomer, editCustomer, exportCustomer } from '@/api/qbs/customer'

export default {
  name: 'Customer',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      customerList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 是否优先字典
      isPriorOptions: [], // 状态字典
      // 状态字典
      statusOptions: [], // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        cnName: undefined,
        cardNo: undefined,
        telphone: undefined,
        status: undefined,
        createTime: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cnName: [
          { required: true, message: this.$t('calling.cnNameEmpty'), trigger: 'blur' }
        ],
        cardNo: [
          { required: true, message: this.$t('calling.cardNoEmpty'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('yes_no').then(response => {
      this.isPriorOptions = response.data
    })
    this.getDicts('status').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    /** 查询客户管理列表 */
    getList() {
      this.loading = true
      listCustomer(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.customerList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 是否优先字典翻译
    isPriorFormat(row, column) {
      return this.selectDictLabel(this.isPriorOptions, row.isPrior)
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        cnName: undefined,
        enName: undefined,
        age: 0,
        sex: '1',
        nation: '汉族',
        isPrior: '1',
        portrait: undefined,
        cardType: undefined,
        cardNo: undefined,
        address: undefined,
        telphone: undefined,
        introduce: undefined,
        status: '0'
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = this.$t('calling.addCustomer')
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.reset()
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = this.$t('calling.changeCustomer')
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            editCustomer(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess(this.$t('loginLog.changeSuccess'))
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addCustomer(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess(this.$t('addSuccess'))
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const ids = row.id || this.ids
      this.$confirm(this.$t('calling.enterDeleteCustomer') + ids + this.$t('equipment.deleteEquipment2'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'

      }).then(function() {
        return delCustomer(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('deleteSuccess'))
      }).catch(function() {
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      if (this.ids.length === 0) {
        this.msgWarning(this.$t('equipment.equipmentInput'))
        return
      }
      const queryParams = this.queryParams
      this.$confirm(this.$t('calling.exportCustomer'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(() => {
        return exportCustomer(queryParams, this.ids)
      }).then(response => {
        this.download(response.data)
      }).catch(function() {
      })
    }
  }
}
</script>
<style lang="scss">
body > div.el-message-box__wrapper > div > div.el-message-box__content > div.el-message-box__container > div.el-message-box__message > p {
  width: 60vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
